package com.pipayshop.service.impl;

import com.pipayshop.domain.StoreCommodityLive;
import com.pipayshop.domain.vo.LivePageVO;
import com.pipayshop.domain.vo.PageDataVO;
import com.pipayshop.domain.vo.StoreCommodityLiveVO;
import com.pipayshop.mapper.PhysicalStoreCommodityLiveMapper;
import com.pipayshop.service.PhysicalStoreCommodityLiveService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.utils.StringUtil;
import com.pipayshop.utils.ValidationUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 实体店住的服务表 服务实现类
 * </p>
 *
 * @author zxb
 * @since 2023-07-29
 */
@Service
public class PhysicalStoreCommodityLiveServiceImpl extends ServiceImpl<PhysicalStoreCommodityLiveMapper, StoreCommodityLive> implements PhysicalStoreCommodityLiveService {

    @Resource
    private PhysicalStoreCommodityLiveMapper shopCommodityLiveMapper;

    /**
     * 查找实体店住的服务列表
     */
    @Override
    public PageDataVO selectShopCommodityLiveVO(Integer limit, Integer pages) {
        Integer integer = shopCommodityLiveMapper.selectAllShopCommodityLiveVO();
        List<StoreCommodityLiveVO> shopCommodityLiveVOS = shopCommodityLiveMapper.selectShopCommodityLiveVO(limit, (pages-1)*limit);
        return new PageDataVO(integer,shopCommodityLiveVOS);
    }

    /**
     * 根据服务id查找服务的详情信息
     */
    @Override
    public StoreCommodityLive selectShopLiveById(String commodityId) {
        StoreCommodityLive shopCommodityLive = shopCommodityLiveMapper.selectOne(new QueryWrapper<StoreCommodityLive>()
                                                                                .eq("commodity_id", commodityId));
        return shopCommodityLive;
    }

    /**
     * 条件筛选查找实体店住的服务列表
     */
    @Override
    public PageDataVO selectShopLiveVOByCondition(LivePageVO livePageVO) {
        Integer integer = shopCommodityLiveMapper.selectAllShopLiveVOByCondition(livePageVO.getCheckInTime(),livePageVO.getDepartureTime(),livePageVO.getAdult(),livePageVO.getChildren());
        List<StoreCommodityLiveVO> shopCommodityLiveVOS = shopCommodityLiveMapper.selectShopLiveVOByCondition(livePageVO.getLimit(),(livePageVO.getPage()-1)*livePageVO.getLimit(), livePageVO.getCheckInTime(),livePageVO.getDepartureTime(),livePageVO.getAdult(),livePageVO.getChildren());
        return new PageDataVO(integer,shopCommodityLiveVOS);
    }

    /**
     * 发布实体店住的服务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertShopLive(StoreCommodityLive shopCommodityLive) {
        // 校验必填字段
        ValidationUtil.validateString(shopCommodityLive.getCommodityId(), "commodity_id_required");
        ValidationUtil.validateString(shopCommodityLive.getShopId(), "shop_id_required");
        ValidationUtil.validatePositiveBigDecimal(shopCommodityLive.getPrice(), "price_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getLand(), "land_area_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getRoom(), "room_count_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getRestRoom(), "restroom_count_cannot_be_negative");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getBed(), "bed_count_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getAdult(), "adult_capacity_cannot_be_negative");
        // 设置商品ID
        shopCommodityLive.setCommodityId(StringUtil.generateShortId());
        // 插入数据
        int result = shopCommodityLiveMapper.insert(shopCommodityLive);
        return result > 0;
    }


    /**
     * 根据房型id更改房型信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateShopLive(StoreCommodityLive shopCommodityLive) {
        // 校验必填字段
        ValidationUtil.validateString(shopCommodityLive.getCommodityId(), "commodity_id_required");
        ValidationUtil.validatePositiveBigDecimal(shopCommodityLive.getPrice(), "price_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getLand(), "land_area_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getRoom(), "room_count_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getRestRoom(), "restroom_count_cannot_be_negative");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getBed(), "bed_count_must_be_positive");
        ValidationUtil.validatePositiveNumber(shopCommodityLive.getAdult(), "adult_capacity_cannot_be_negative");
        // 执行更新操作
        int result = shopCommodityLiveMapper.update(null, new UpdateWrapper<StoreCommodityLive>()
                .eq("commodity_id", shopCommodityLive.getCommodityId())
                .set("detail", shopCommodityLive.getDetail())
                .set("tag_list", shopCommodityLive.getTagList())
                .set("image_list", shopCommodityLive.getImageList())
                .set("land", shopCommodityLive.getLand())
                .set("room", shopCommodityLive.getRoom())
                .set("rest_room", shopCommodityLive.getRestRoom())
                .set("bed", shopCommodityLive.getBed())
                .set("adult", shopCommodityLive.getAdult())
                .set("children", shopCommodityLive.getChildren())
                .set("restricted", shopCommodityLive.getRestricted())
                .set("check_in_time", shopCommodityLive.getCheckInTime())
                .set("departure_time", shopCommodityLive.getDepartureTime())
                .set("basics", shopCommodityLive.getBasics())
                .set("bath", shopCommodityLive.getBath())
                .set("status", shopCommodityLive.getStatus())
                .set("price", shopCommodityLive.getPrice())
                .set("avatar_imag", shopCommodityLive.getAvatarImag())
                .set("appliance", shopCommodityLive.getAppliance()));

        return result > 0;
    }

}
